export function renderTime(date) {
  var dateee = new Date(date).toJSON()
  return new Date(+new Date(dateee) + 8 * 3600 * 1000)
    .toISOString()
    .replace(/T/g, ' ')
    .replace(/\.[\d]{3}Z/, '')
}
export function addZero(num) {
  if (parseInt(num) < 10) {
    num = '0' + num
  }
  return num
}
export function formatMsToDate(ms) {
  if (ms) {
    var oDate = new Date(ms),
      oYear = oDate.getFullYear(),
      oMonth = oDate.getMonth() + 1,
      oDay = oDate.getDate(),
      oHour = oDate.getHours(),
      oMin = oDate.getMinutes(),
      oSen = oDate.getSeconds(),
      oTime =
        oYear +
        '-' +
        addZero(oMonth) +
        '-' +
        addZero(oDay) +
        ' ' +
        addZero(oHour) +
        ':' +
        addZero(oMin) +
        ':' +
        addZero(oSen)
    return oTime
  } else {
    return ''
  }
}
export function dateSwitch(date) {
  let string =
    date.toLocaleDateString('en-GB').split('/').reverse().join('') +
    date.toLocaleTimeString('en-GB').split(':').join('')
  //  let newString =
  return string.substring(0, string.length - 4)
}
export const chunk = (arr, size) => {
  if (!arr.length || size < 1) return []
  let list = [],
    index = 0,
    resIndex = 0,
    len = arr.length
  while (index < len) {
    list[resIndex++] = arr.slice(index, (index += size))
  }
  return list
}
export function dateFtt(fmt, date) {
  //
  var o = {
    'M+': date.getMonth() + 1, //月份
    'd+': date.getDate(), //日
    'h+': date.getHours(), //小时
    'm+': date.getMinutes(), //分
    's+': date.getSeconds(), //秒
    'q+': Math.floor((date.getMonth() + 3) / 3), //季度
    S: date.getMilliseconds() //毫秒
  }
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  for (var k in o)
    if (new RegExp('(' + k + ')').test(fmt))
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
      )
  return fmt
}
export function debounce(fn, delay) {
  let timer
  return function () {
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      fn(...arguments)
    }, delay)
  }
}
export function assignUniqueIds(treeData) {
  let idCounter = 1

  function assignIds(nodes) {
    for (const node of nodes) {
      node.id = idCounter++

      if (node.children && node.children.length > 0) {
        assignIds(node.children)
      }
    }
  }

  assignIds(treeData)
}
export function hasProperty(obj, property) {
  return property in obj
}

export function assignByParams(treeData, param, data) {
  let obj = {}
  function assignIds(nodes) {
    for (const node of nodes) {
      // node.id = idCounter++
      // node['blkcd']
      if (hasProperty(node, param)) {
        if (node[param] === data) {
          obj = node
        }
      }
      if (node.children && node.children.length > 0) {
        assignIds(node.children)
      }
    }
  }

  assignIds(treeData)
  return obj
}
export function generateHourlyTimeArray(startStr, endStr) {
  const format = (d) => {
    return d.toISOString().replace('T', ' ').substring(0, 19)
  }

  const startTime = new Date(startStr)
  const endTime = new Date(endStr)
  let currentTime = startTime
  const timesArray = []

  while (currentTime <= endTime) {
    timesArray.push(format(new Date(currentTime)))
    currentTime.setHours(currentTime.getHours() + 1)
  }

  return timesArray
}

export function generateHourlyTimestamps(startTime, endTime) {
  var start = new Date(startTime)
  var end = new Date(endTime)
  var timestamps = []

  if (start > end) {
    throw new Error('StartTime must be before EndTime')
  }

  while (start <= end) {
    timestamps.push(
      start.toISOString().split('T')[0] +
        ' ' +
        (start.getHours() + ':') +
        ('0' + start.getMinutes()).slice(-2) +
        ':' +
        ('0' + start.getSeconds()).slice(-2)
    )
    start.setHours(start.getHours() + 1)
  }
  return timestamps
}
